package com.teufandsub.socialproximity.gui;

import java.util.ArrayList;
import java.util.AbstractMap.SimpleEntry;

import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.teufandsub.socialproximity.R;
import com.teufandsub.socialproximity.controller.Controller;
import com.teufandsub.socialproximity.logic.Message;
import com.teufandsub.socialproximity.logic.User;

/**
 * @author Bastien Mathias <bastienmathias@gmail.com>
 * @author Antoine Teuf <antoine.teuf@gmail.com>
 * @version 1.0
 * @since 2013-05-22
 * 
 * The Chatting Message Adapter use to fill up the view.
 */

public class ChattingMessageAdapter extends ArrayAdapter<Message> {

	private ArrayList<Message> messages;
	private Context context;
	private Controller controller;
	
	
	/**
	 * Constructor of the Adapter.
	 * 
	 * @param context Context of the Applications
	 * @param messages ArrayList<Message> All messages to set 
	 */
	public ChattingMessageAdapter(Context context, ArrayList<Message> messages) {
		super(context,0,messages);
		this.messages = messages;
		this.context = context;
		controller = Controller.getInstance();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflater = LayoutInflater.from(context);       
        Message message = messages.get(position);
		SimpleEntry<String,JSONObject> prefs = controller.getSessionInfo(context);
		String currentUsername="";
		try {
			currentUsername = prefs.getValue().getString(User.USERNAME_NAME);
		} catch (Exception e) {
		}
        if(message.getAuthor().equals(currentUsername)){
        	convertView = inflater.inflate(R.layout.your_message_layout, parent, false);
        }else{
        	convertView = inflater.inflate(R.layout.friend_message_layout, parent, false);
        }
        
        TextView messageTV = (TextView) convertView.findViewById(R.id.message_text);
        messageTV.setText(message.getContent());
        

		return convertView;
	}
	
}
